import { hashSync } from 'bcryptjs';
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, SchemaTypes } from 'mongoose';

export type UserDocument = User & Document;

@Schema({ timestamps: { createdAt: 'createdAt', updatedAt: 'updatedAt' } })
export class User {
  @Prop()
  username?: string;

  @Prop()
  sort?: number;

  @Prop({ type: SchemaTypes.ObjectId, ref: () => 'Role' })
  roleId?: number;

  @Prop({
    select: false,
    get: (v: string): string => v,
    set: (v: string): string => hashSync(v),
  })
  password?: string;
}

export const UserSchema = SchemaFactory.createForClass(User);
